from django.db import models
from django.conf import settings


class TimestampMixin(models.Model):
    """时间戳混入 - 提供创建和更新时间字段"""
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        abstract = True


class UserTrackingMixin(models.Model):
    """用户追踪混入 - 提供创建者和更新者字段"""
    created_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,  # 使用settings.AUTH_USER_MODEL引用自定义用户模型
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="%(class)s_created",
        verbose_name="创建者"
    )
    updated_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,  # 使用settings.AUTH_USER_MODEL引用自定义用户模型
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="%(class)s_updated",
        verbose_name="更新者"
    )

    class Meta:
        abstract = True


class BaseModel(TimestampMixin, UserTrackingMixin):
    """完整基础模型 - 包含时间戳和用户追踪"""
    class Meta:
        abstract = True
        ordering = ['-created_at']
